package openGl;

import android.opengl.GLSurfaceView;
import android.opengl.GLU;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import camera.OpenGl.utils.ByteBufferUtils;

/**
 * Created by Administrator on 2017/12/19.
 */

public class GLRender implements GLSurfaceView.Renderer {
    //绘制点的坐标点
    private int  rotate=360;
    private float[] pointArray=new float[]{0.0f,0.0f,0.0f};
    private float[] trianglesArray=new float[]{
            -0.5f, -0.5f, 0f,
            0.5f, -0.5f, 0f,
            -0.5f, 0.5f, 0f,
            0.5f, 0.5f, 0f,

    };
    private float[] colorArray=new float[]{
            1,0,0,
            1,1,0,
            1,0,1,
            0,0,1
    };
    // 定义立方体的8个顶点  
    float[] cubeVertices2 = {
            //左面 
            -0.5f,0.5f,0.5f,
            -0.5f,-0.5f,0.5f,
            -0.5f,0.5f,-0.5f,
            -0.5f,-0.5f,-0.5f,

            //右面
            0.5f, 0.5f,0.5f,
            0.5f,-0.5f,0.5f,
            0.5f,-0.5f,-0.5f,
            0.5f,0.5f,-0.5f ,

            //前面
            -0.5f,0.5f,0.5f,
            -0.5f,-0.5f,0.5f,
            0.5f,-0.5f,0.5f,
            0.5f, 0.5f,0.5f,

            //后面 
            0.5f,-0.5f,-0.5f,
            0.5f,0.5f,-0.5f,
            -0.5f,0.5f,-0.5f,
            -0.5f,-0.5f,-0.5f,

            //上面
            -0.5f,0.5f,0.5f,
            0.5f, 0.5f,0.5f,
            0.5f,0.5f,-0.5f,
            -0.5f,0.5f,-0.5f,

            //下面
            -0.5f,-0.5f,0.5f,
            0.5f,-0.5f,0.5f,
            0.5f,-0.5f,-0.5f,
            -0.5f,-0.5f,-0.5f
    };
    //  颜色数组 
    float []  cubeColors = {
            1f,0f,0f,1f ,
            0f,1f,0f,1f,
            0f,0f,1f,1f,
            1f,0f,0f,1f,

            1f,0f,0f,1f ,
            0f,1f,0f,1f,
            0f,0f,1f,1f,
            1f,0f,0f,1f,

            1f,0f,0f,1f ,
            0f,1f,0f,1f,
            0f,0f,1f,1f,
            1f,0f,0f,1f,

            1f,0f,0f,1f ,
            0f,1f,0f,1f,
            0f,0f,1f,1f,
            1f,0f,0f,1f,


            1f,0f,0f,1f ,
            0f,1f,0f,1f,
            0f,0f,1f,1f,
            1f,0f,0f,1f,

            1f,0f,0f,1f ,
            0f,1f,0f,1f,
            0f,0f,1f,1f,
            1f,0f,0f,1f,
    };
    private FloatBuffer pointbuffer;
    private FloatBuffer trianglesBuffer;
    private FloatBuffer colorBuffer;
    private ShortBuffer shortIndex;
    
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        //设置窗体的背景色为黑色
        gl.glClearColor(0,0,0,0);
      
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0,0,width,height);
    }
    //纹理数组
    private int[] textureData=new int[1];
    @Override
    public void onDrawFrame(GL10 gl) {
        
        //清除屏幕颜色
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        //设置顶点可用
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        //设置顶点颜色可用
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        // 启动纹理坐标数据
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        //开启纹理
        gl.glEnable(GL10.GL_TEXTURE_2D);
        //创建纹理
        gl.glGenTextures(1,textureData,0);
        //绑定纹理
        gl.glBindTexture(GL10.GL_TEXTURE_2D,textureData[0]);
        gl.glLoadIdentity();
        gl.glRotatef(rotate,0,1,0);
        //设置颜色数据
        gl.glColorPointer(4,GL10.GL_FLOAT,0,colorBuffer);
        //设置顶点坐标
        gl.glVertexPointer(3, GL10.GL_FLOAT,0,trianglesBuffer);
        //设置点的颜色
      //  gl.glColor4f(1,0,0,
        // 0);
        //设置点的大小
       // gl.glPointSize(360f);
        //绘制顶点
       
//        gl.glTranslatef(0.2f,0.2f,1.0f);
//        gl.glRotatef(-20, 1f, 0f, 0f);
//        gl.glScalef(1.5f,1.5f,1.5f);
        GLU.gluLookAt(gl, 0f, 0f, 0f,  0f, 0f, -1f,  0f, 1f, 0f);
        //顺序绘制
        gl.glDrawElements(GL10.GL_TRIANGLES,indices.length,GL10.GL_UNSIGNED_SHORT,shortIndex);
       // gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,24);
        
        //禁用顶点
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
        gl.glFinish();
        rotate-=1;
    }

    public GLRender() {
        //pointbuffer=ByteBufferUtils.makeFloteByteBuffer(pointArray);
        trianglesBuffer= ByteBufferUtils.makeFloteByteBuffer(cubeVertices2);
        colorBuffer=ByteBufferUtils.makeFloteByteBuffer(cubeColors);
        shortIndex=ByteBufferUtils.makeShortBuffer(indices);
    }
    //索引数组
    private short[] indices={
            0,1,2,
            0,2,3,

            4,5,6,
            4,6,7,

            8,9,10,
            8,10,11,

            12,13,14,
            12,14,15,

            16,17,18,
            16,18,19,

            20,21,22,
            20,22,23,
    };
}
